﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Module.Common.Models;
using Module.Core.Common;
using Module.Core.DAO;
using Module.Core.EntitiesInfo;

namespace Module.Core.DAO
{
    public class AdvertisementDAO
    {
        #region Fields

        protected string connectionStringName;

        #endregion

        #region Methods

        /// <summary>
        /// Saves a record to the Advertisement table.
        /// </summary>
        public virtual int Insert(AdvertisementInfo advertisementInfo)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{				
				new SqlParameter("@Title", advertisementInfo.Title),
				new SqlParameter("@Description", advertisementInfo.Description),
				new SqlParameter("@MediaSrc", advertisementInfo.MediaSrc),
				new SqlParameter("@Width", advertisementInfo.Width),
				new SqlParameter("@Height", advertisementInfo.Height),
				new SqlParameter("@MediaType", advertisementInfo.MediaType),
				new SqlParameter("@Target", advertisementInfo.Target),
				new SqlParameter("@Priority", advertisementInfo.Priority),
				new SqlParameter("@CreatedBy", advertisementInfo.CreatedBy),
				new SqlParameter("@CreatedOn", advertisementInfo.CreatedOn),
				new SqlParameter("@StartDate", advertisementInfo.StartDate),
				new SqlParameter("@EndDate", advertisementInfo.EndDate),
				new SqlParameter("@CustomerID", advertisementInfo.CustomerID),
				new SqlParameter("@Click", advertisementInfo.Click),
				new SqlParameter("@ViewCount", advertisementInfo.ViewCount),
				new SqlParameter("@IsEnable", advertisementInfo.IsEnable),
				new SqlParameter("@PositionID", advertisementInfo.PositionID)
			};

            return (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "Advertisement_Insert", parameters);
        }

        /// <summary>
        /// Updates a record in the Advertisement table.
        /// </summary>
        public virtual void Update(AdvertisementInfo advertisementInfo)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@AdvertisementID", advertisementInfo.AdvertisementID),
				new SqlParameter("@Title", advertisementInfo.Title),
				new SqlParameter("@Description", advertisementInfo.Description),
				new SqlParameter("@MediaSrc", advertisementInfo.MediaSrc),
				new SqlParameter("@Width", advertisementInfo.Width),
				new SqlParameter("@Height", advertisementInfo.Height),
				new SqlParameter("@MediaType", advertisementInfo.MediaType),
				new SqlParameter("@Target", advertisementInfo.Target),
				new SqlParameter("@Priority", advertisementInfo.Priority),
				new SqlParameter("@CreatedBy", advertisementInfo.CreatedBy),
				new SqlParameter("@CreatedOn", advertisementInfo.CreatedOn),
				new SqlParameter("@StartDate", advertisementInfo.StartDate),
				new SqlParameter("@EndDate", advertisementInfo.EndDate),
				new SqlParameter("@CustomerID", advertisementInfo.CustomerID),
				new SqlParameter("@Click", advertisementInfo.Click),
				new SqlParameter("@ViewCount", advertisementInfo.ViewCount),
				new SqlParameter("@IsEnable", advertisementInfo.IsEnable),
				new SqlParameter("@PositionID", advertisementInfo.PositionID)
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Advertisement_Update", parameters);
        }

        /// <summary>
        /// Deletes a record from the Advertisement table by its primary key.
        /// </summary>
        public virtual void Delete(int advertisementId, int positionId)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@AdvertisementID", advertisementId),
                new SqlParameter("@PositionID", positionId)                
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Advertisement_Delete", parameters);
        }

        /// <summary>
        /// Selects a single record from the Advertisement table.
        /// </summary>
        public virtual AdvertisementInfo Select(int advertisementID)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@AdvertisementID", advertisementID)
			};

            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Advertisement_Select", parameters))
            {
                if (dataReader.Read())
                {
                    return MakeAdvertisementInfo(dataReader);
                }
                else
                {
                    return null;
                }
            }
        }
        public virtual void WriteXmlBanner(string fileNameXML, string pathWriteXML)
        {

            using (DataSet ds = SqlClientUtility.ExecuteDataSet(connectionStringName, CommandType.StoredProcedure, "Advertisement_XmlBanner"))
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    ds.WriteXml(pathWriteXML + "\\" + fileNameXML + ".xml");
                }
                else
                {
                    ComomFunctions.DeleteFile(pathWriteXML + "\\" + fileNameXML + ".xml");
                }
            }
        }

        /// <summary>
        /// Selects all records from the Advertisement table.
        /// </summary>
        public CHRTList<AdvertisementInfo> SelectAll()
        {
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Advertisement_SelectAll"))
            {
                CHRTList<AdvertisementInfo> advertisementInfoList = new CHRTList<AdvertisementInfo>();
                while (dataReader.Read())
                {
                    AdvertisementInfo advertisementInfo = MakeAdvertisementInfo(dataReader);
                    advertisementInfoList.Add(advertisementInfo);
                }

                return advertisementInfoList;
            }
        }

        /// <summary>
        /// Creates a new instance of the Advertisement class and populates it with data from the specified SqlDataReader.
        /// </summary>
        protected virtual AdvertisementInfo MakeAdvertisementInfo(SqlDataReader dataReader)
        {
            AdvertisementInfo advertisementInfo = new AdvertisementInfo();
            advertisementInfo.AdvertisementID = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT.ADVERTISEMENTID, 0);
            advertisementInfo.Title = SqlClientUtility.GetString(dataReader, DbConstants.ADVERTISEMENT.TITLE, String.Empty);
            advertisementInfo.Description = SqlClientUtility.GetString(dataReader, DbConstants.ADVERTISEMENT.DESCRIPTION, String.Empty);
            advertisementInfo.MediaSrc = SqlClientUtility.GetString(dataReader, DbConstants.ADVERTISEMENT.MEDIASRC, String.Empty);
            advertisementInfo.Width = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT.WIDTH, 0);
            advertisementInfo.Height = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT.HEIGHT, 0);
            advertisementInfo.MediaType = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT.MEDIATYPE, 0);
            advertisementInfo.Target = SqlClientUtility.GetString(dataReader, DbConstants.ADVERTISEMENT.TARGET, String.Empty);
            advertisementInfo.Priority = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT.PRIORITY, 0);
            advertisementInfo.CreatedBy = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT.CREATEDBY, 0);
            advertisementInfo.CreatedOn = SqlClientUtility.GetDateTime(dataReader, DbConstants.ADVERTISEMENT.CREATEDON, DateTime.Now);
            advertisementInfo.StartDate = SqlClientUtility.GetDateTime(dataReader, DbConstants.ADVERTISEMENT.STARTDATE, DateTime.Now);
            advertisementInfo.EndDate = SqlClientUtility.GetDateTime(dataReader, DbConstants.ADVERTISEMENT.ENDDATE, DateTime.Now);
            advertisementInfo.CustomerID = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT.CUSTOMERID, 0);
            advertisementInfo.Click = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT.CLICK, 0);
            advertisementInfo.ViewCount = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT.VIEWCOUNT, 0);
            advertisementInfo.IsEnable = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT.ISENABLE, 0);
            advertisementInfo.PositionID = SqlClientUtility.GetInt32(dataReader, DbConstants.ADVERTISEMENT.POSITIONID, 0);

            return advertisementInfo;
        }

        #endregion
    }
}
